package your.rpg.namespace;

/*events to implement:				systems required:
 * add item 						inventory
 * remove item						inventory
 * increase stats max value			character
 * decrease stats max value			character
 * increase weapon experience		character
 * decrease weapon experience		character
 * add character to party			character/party
 * remove character from party		character/party
 * start battle						battle				
 * add point of interest			interest points
 * remove point of interest			interest points
 * add movepoint					move points
 * remove movepoint					points									
 */

public class EventHandler {
	private LevelData _leveldata;
	private String currentevent = "";
	private int currentindex = 0;
	private int currentlevel = 0;
	private String action = "";
	private String actioncontent = "";
	private String option1 = "";
	private String option2 = "";
	private String option3 = "";
	private int selectedOption = 0;
	private boolean isPlayingEvent = false;
	private int skipAmount = 0;
	
	public EventHandler(LevelData data){
		_leveldata = data;
	}
	
	public void setCurrentEvent(String name){
		currentevent = name;
	}
	
	public void setCurrentLevel(int name){
		currentlevel = name;
	}
	
	public String getAction(){
		return action;
	}
	
	public int getSkipAmount(){
		return skipAmount;
	}
	
	public String getActionContent(){
		return actioncontent;
	}
	
	public void setPlaying(){
		isPlayingEvent = true;
	}
	
	public void getNextEvent(){
		if(isPlayingEvent == true)
		{
			String action1 = _leveldata.getEvent(currentlevel,currentevent,currentindex);
			executeEvent(action1);
		}
	}
	
	public String getEvent(int index){//this function is only used to get the conversation options.
		return _leveldata.getEvent(currentlevel,currentevent,currentindex+index);
	}
	
	public void executeEvent(String action1){
		//check string to see which action has to be performed
		if(action.equals("text3"))
		{
			if(action1.contains("t001") && selectedOption == 1)
			{
				action = "text3";
				actioncontent = action1.substring(23);
				skipAmount = Integer.parseInt(action1.substring(14, 16));
			}
			else if(action1.contains("t002") && selectedOption == 2)
			{
				action = "text3";
				actioncontent = action1.substring(23);
				skipAmount = Integer.parseInt(action1.substring(14, 16));
			}
			else if(action1.contains("t003") && selectedOption == 3)
			{
				action = "text3";
				actioncontent = action1.substring(23);
				skipAmount = Integer.parseInt(action1.substring(14, 16));
			}
			else if(action1.contains("action=endtext:"))
			{
				nextEvent();
				actioncontent = "";
				action = "";
			}
		}
		else if(action.equals("text2"))
		{ //replace with executeEvent(action1) if that works
			//show text of selected option
			if(action1.contains("action=text2:"))
			{
				action = "text3";
			}
		}
		else if(action1.contains("action=text:"))
		{
		/*eventlist.addEvent("event_start", "action=text: no; test text"); -no conversation choise
		eventlist.addEvent("event_start", "action=text: ja; test text with choises"); conversation choise
		eventlist.addEvent("event_start", "option1"); option 1
		eventlist.addEvent("event_start", "action=text2: t001; text1"); text option 1
		eventlist.addEvent("event_start", "action=text2: t002; text2"); text option 2
		eventlist.addEvent("event_start", "action=endtext:"); end text --- only if conversation choises
		*/
			if(action1.substring(13,15).equals("no"))
			{
				action = "text";
				actioncontent = action1.substring(17);
			}
			else if(action1.substring(13,15).equals("ja"))
			{
				action = "text2";
				actioncontent = action1.substring(17);
				option1 = getEvent(1);
				option2 = getEvent(2);
				option3 = getEvent(3);
			}
		}
		else if(action1.contains("action=moveobject:"))
		{//action=moveobject: 012,240 0
			action = "moveobject";
			int index = Integer.parseInt(action1.substring(27));
			int xpos = Integer.parseInt(action1.substring(19, 22));
			int ypos = Integer.parseInt(action1.substring(23, 26));
			_leveldata.setObjectPos(currentlevel,index,xpos,ypos);
			nextEvent();
		}
		else if(action1.contains("action=changeimage:"))
		{//action=changeimage: 0x7f020004 henk ----- imageid must be a decimal number
			action = "changeimage";
			int index = Integer.parseInt(action1.substring(31));
			int imageid = Integer.parseInt(action1.substring(20, 30));
			_leveldata.setObjectImage(currentlevel, index, imageid);
			nextEvent();
		}
		else if(action1.contains("action=addobject:"))
		{//action=addobject: 2130837504,100,300,klaas
			action = "addobject";
			String naam = action1.substring(37);
			int imageid = Integer.parseInt(action1.substring(18, 28));
			int xpos = Integer.parseInt(action1.substring(29, 32));
			int ypos = Integer.parseInt(action1.substring(33, 36));
			_leveldata.addObject(currentlevel, naam, imageid, xpos, ypos);
			nextEvent();
		}
		else if(action1.contains("action=removeobject:"))
		{//action=removeobject: 1
			action = "removeobject";
			int index = Integer.parseInt(action1.substring(21));
			_leveldata.removeObject(currentlevel,index);
			nextEvent();
		}
		else if(action1.contains("action=changelevel:"))
		{//action=changelevel: leveltwo  ----- after changelevel there cannot be another action
			action = "changelevel";
			for(int i = 0; i < _leveldata.getLevelAmount(); i++)
			{
				if(_leveldata.getLevelNumber(i) == Integer.parseInt(action1.substring(20)))
				{
					currentlevel = Integer.parseInt(action1.substring(20));
					currentindex = 0;
					action = "";
					actioncontent = "";
					isPlayingEvent = false;
				}
			}
		}
		else if(action1.contains("No action found"))
		{
			action = "";
			actioncontent = "";
			currentindex = 0;
			isPlayingEvent = false;
		}
	}
	
	public void nextEvent(){
		currentindex++;
	}
	
	public void nextEventSkip(int amount){
		currentindex += amount;
	}
	
	public int getCurrentLevel(){
		return currentlevel;
	}
	
	public boolean getPlayingEvent(){
		return isPlayingEvent;
	}
	
	public void setSelectedOption(int number){
		selectedOption = number;
	}
	
	public String getTextOption(int number){
		String option = "";
		if(number == 1)
		{
			option = option1;
		}
		else if(number == 2)
		{
			option = option2;
		}
		else if(number == 3)
		{
			option = option3;
		}
		return option;
	}
}
